package com.jintoufs.web.action.customer;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.QueryRule;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.service.customer.CustomerService;
import com.jintoufs.web.action.shiro.bind.annotation.CurrentUser;
import com.jintoufs.web.action.support.JqgridPage;
import com.jintoufs.web.action.support.SupportAction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("share-user")
public class ShareUserAction extends SupportAction {

    @Resource
    private CustomerService customerService;

    @RequestMapping("list")
    public String list(){
        return "customer/share-list";
    }
    @RequestMapping("/query")
    @ResponseBody
    public JqgridPage query(int page, int rows, String sidx, String sord, String filters,@CurrentUser Customer customer) {
        PageHelper.startPage(page, rows);
        QueryFilters filters_ = null;
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }
        QueryRule queryRule;
        queryRule = new QueryRule();
        queryRule.setField("type");
        queryRule.setOp("eq");
        queryRule.setData("1");
        filters_.getRules().add(queryRule);
        QueryRule queryRule2;
        queryRule2 = new QueryRule();
        queryRule2.setField("recommend_id");
        queryRule2.setOp("eq");
        queryRule2.setData(customer.getId());
        filters_.getRules().add(queryRule2);

        filters_.setSidx(sidx);
        filters_.setSord(sord);
        List<Customer> list = customerService.getByFilter(filters_);
        // 用PageInfo对结果进行包装
        PageInfo pageInfo = new PageInfo(list);
        return new JqgridPage(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(),
                pageInfo.getList());
    }
}
